package com.pwt.repository;

import com.pwt.entity.ZzWatchCharts;
import com.pwt.repository.base.DataRepositoryExtension;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dw
 * @date 2022/1/13 16:33
 * @Description
 */
public class ZzWatchChartsRepositoryExtensionImpl extends DataRepositoryExtension<ZzWatchCharts, String> implements ZzWatchChartsRepositoryExtension{

    @Override
    public List<ZzWatchCharts> queryZzWatchChartsBykAndK0010AndkAndK0004(List<Integer> k0010, Date startTime, Date endTime) {
        String sql = "select k0010,k1002,k0014,(case when  RIGHT_STATUS='SOMEOK' then 1 when RIGHT_STATUS='NOK' then 2 when RIGHT_STATUS='BAD' then 3 end) as  attribute from zz_watch_charts where K0010 in (:k0010) and DATEDIFF(day, K0004, :start)<=0 and DATEDIFF(day, K0004, :end) >=0";
        Map<String,Object> map = new HashMap<>();
        map.put("k0010",k0010);
        map.put("start",startTime);
        map.put("end",endTime);
        return super.queryCustomBeanListBySql(ZzWatchCharts.class,sql,map);
    }


    public Map<String, Object> queryStatistics(Integer k0010, Date beginTime, Date endTime){
        String sql ="SELECT \n" +
                "(SELECT  top 1 K1002 from zz_watch_charts where K0010=?0  and  K0004<=?1 and K1002 is not null order by K0004 desc ) k1002,"+
                " count(ok) ok,\n" +
                " count(SOMEOK) SOMEOK,\n" +
                " count(NOK) NOK,\n" +
                " count(BAD) BAD,\n" +
                " COUNT(*) TOTAL\n" +
                " from (\n" +
                " SELECT \n" +
                " (case when (RIGHT_STATUS='OK') then   RIGHT_STATUS else null end) as ok,\n" +
                " (case when (RIGHT_STATUS='SOMEOK') then   RIGHT_STATUS else null end) as SOMEOK,\n" +
                " (case when (RIGHT_STATUS='NOK') then   RIGHT_STATUS else null end) as NOK,\n" +
                " (case when (RIGHT_STATUS='BAD') then   RIGHT_STATUS else null end) as BAD\n" +
                "from zz_watch_charts where K0010= ?2 and K0004>=?3 and K0004<=?4\n" +
                ") a ";

        return super.queryMapBySql(sql,k0010,endTime,k0010,beginTime,endTime);
    }


    public String queryK1002(Integer k1000){
        String sql = "select TEBEZEICH from TEIL where TETEIL=?0 ";
        return super.queryString(sql,k1000);
    }
}
